﻿#include "FilenameDialog.h"
#include <QFileDialog>
#include <QMenu>
#include <QDateTime>
#include <QDebug>
#include <QSettings>
#include <QStandardPaths>
FileNameDialog::FileNameDialog(QWidget *parent) :
    QDialog(parent)
{
    m_rowCount = 4;
    m_colCount = 8;
    initContent();
    initConnection();
    QTableWidgetItem *newItem = new QTableWidgetItem("ABC");
         tableWidget->setItem(1, 1, newItem);
}

void FileNameDialog::initContent()
{
    resize(850, 350);
    actionApplyToAll = new QAction(this);
    actionApplyToAll->setText("Apply to All");
    actionApplyToCurrentRow = new QAction(this);
    actionApplyToCurrentRow->setText("Apply to Current Row");
    actionApplyToCurrentCol = new QAction(this);
    actionApplyToCurrentCol->setText("Apply to Current Col");

    verticalLayout = new QVBoxLayout(this);

    horizontalLayout = new QHBoxLayout();
    pushButtonDir = new QPushButton(this);
    pushButtonDir->setText(QString::fromUtf8(u8"保存路径"));
    horizontalLayout->addWidget(pushButtonDir);

    lineEditDir = new QLineEdit(this);
    lineEditDir->setReadOnly(true);
    horizontalLayout->addWidget(lineEditDir);
    m_dir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    lineEditDir->setText(m_dir);

    verticalLayout->addLayout(horizontalLayout);

    tableWidget = new QTableWidget(this);
    QStringList list;
    list << "0" << "1" << "2" << "3";
    tableWidget->setVerticalHeaderLabels(list);
    list << "4" << "5" << "6" << "7";
    tableWidget->setHorizontalHeaderLabels(list);
    tableWidget->setColumnCount(m_colCount);
    tableWidget->setRowCount(m_rowCount);
    for(int col = 0; col < m_colCount; col++)
    {
        for(int row = 0; row < m_rowCount; row++)
        {
            QTableWidgetItem * p = new QTableWidgetItem("%Rx%C_%Y%M%D,%H:%m", QTableWidgetItem::Type);
            tableWidget->setItem(row, col, p);
        }
    }

    verticalLayout->addWidget(tableWidget);

    horizontalLayout_2 = new QHBoxLayout();
    horizontalLayout_2->addStretch(10);

    pushButtonOK = new QPushButton(this);
    pushButtonOK->setText(QString::fromUtf8(u8"确定"));
    horizontalLayout_2->addWidget(pushButtonOK);

    verticalLayout->addLayout(horizontalLayout_2);

    label = new QLabel(this);
    verticalLayout->addWidget(label);

    QString comment = QString::fromUtf8(u8"文件名设置格式说明 \n"
                                        "%R : 行号（0，1，2，3，4）\t\t\t\t %C : 列号（0，1，2，3，4，5，6，7）\n"
                                        "%Y : 年份（2015，2016...）\t\t\t\t %M : 月份（1，2，3，...，12）\n"
                                        "%D ：日期（1，2，3，...，31）\t\t\t\t %H ：时（1，2，3，...，23）\n"
                                        "%m : 分（1，2，3，...，59）\t\t\t\t %s ：秒（1，2，3，...，59）");
    label->setText(comment);
}

void FileNameDialog::initConnection()
{
    tableWidget->addAction(actionApplyToAll);
    tableWidget->addAction(actionApplyToCurrentCol);
    tableWidget->addAction(actionApplyToCurrentRow);
    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);

    connect(actionApplyToAll, SIGNAL(triggered()), this, SLOT(applyToAll()));
    connect(actionApplyToCurrentCol, SIGNAL(triggered()), this, SLOT(applyToCol()));
    connect(actionApplyToCurrentRow, SIGNAL(triggered()), this, SLOT(applyToRow()));
    connect(pushButtonOK, SIGNAL(clicked()), this, SLOT(hide()));

    connect(pushButtonDir, SIGNAL(clicked()), this, SLOT(setDir()));
}

void FileNameDialog::loadSettings(QSettings &settings)
{
    m_colCount = settings.value("NozzleHolder/colCount", 8).toInt();
    m_rowCount = settings.value("NozzleHolder/rowCount", 4).toInt();

    m_dir = settings.value("File/path", m_dir).toString();
    lineEditDir->setText(m_dir);
    QString key;
    QString fileName;
    for(int row = 0; row < m_rowCount; row ++)
    {
        for(int col = 0; col < m_colCount; col ++)
        {
             key = QString("File/nozzle%1%2").arg(row).arg(col);
             fileName = settings.value(key, "").toString();
             QTableWidgetItem *item = new QTableWidgetItem(fileName);
             tableWidget->setItem(row, col, item);
        }
    }
}

void FileNameDialog::writeSettings(QSettings &settings)
{
    settings.setValue("File/path", m_dir);
    QString key;
    QString fileName;

    for(int row = 0; row < m_rowCount; row ++)
    {
        for(int col = 0; col < m_colCount; col ++)
        {
            QTableWidgetItem *p = tableWidget->item(row, col);
            if(p == NULL)
            {
                fileName = "";
            }
            else
            {
                fileName = p->text();
            }
            key = QString("File/nozzle%1%2").arg(row).arg(col);
            settings.setValue(key, fileName);
        }
    }
}

QString FileNameDialog::getDir() const
{
    return m_dir;
}

void FileNameDialog::applyToAll(void)
{
    QString str;
    QTableWidgetItem *p = tableWidget->currentItem();
    if(p == NULL)
    {
        str = " ";
    }
    else
    {
        str = p->text();
    }
    for(int col = 0; col < m_colCount; col++)
    {
        for(int row = 0; row < m_rowCount; row++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(str);
            tableWidget->setItem(row, col, item);
        }
    }

}

QString FileNameDialog::getFileName(int row, int col) const
{
    if(row < 0 || row >= m_rowCount)
    {
        return "out of row range";
    }
    if(col < 0 || col >= m_colCount)
    {
        return "out of col range";
    }
    QString str;
    QTableWidgetItem *p = tableWidget->item(row, col);
    if(p == NULL)
    {
        str = "%R_%C";
    }
    else
    {
        str = p->text();
    }
    str.replace(QString("%R"), QString::number(row+1), Qt::CaseInsensitive);
    str.replace(QString("%C"), QString::number(col+1), Qt::CaseInsensitive);

    QDateTime date = QDateTime::currentDateTime();
    str.replace(QString("%Y"), date.toString("yyyy"), Qt::CaseInsensitive);
    str.replace(QString("%M"), date.toString("MM"), Qt::CaseSensitive);
    str.replace(QString("%D"), date.toString("dd"), Qt::CaseInsensitive);
    str.replace(QString("%H"), date.toString("hh"), Qt::CaseInsensitive);
    str.replace(QString("%m"), date.toString("mm"), Qt::CaseSensitive);
    str.replace(QString("%s"), date.toString("ss"), Qt::CaseInsensitive);
    qDebug() << str;
    return str;
}

void FileNameDialog::applyToRow(void)
{
    int row = tableWidget->currentRow();
    QString str;
    QTableWidgetItem *p = tableWidget->currentItem();
    if(p == NULL)
    {
        str = " ";
    }
    else
    {
        str = p->text();
    }
    for(int col = 0; col < m_colCount; col++)
    {
        QTableWidgetItem *item = new QTableWidgetItem(str);
        tableWidget->setItem(row, col, item);
    }
}

void FileNameDialog::applyToCol(void)
{
    int col = tableWidget->currentColumn();
    QString str;
    QTableWidgetItem *p = tableWidget->currentItem();
    if(p == NULL)
    {
        str = "";
    }
    else
    {
        str = p->text();
    }
    for(int row = 0; row < m_rowCount; row++)
    {
        QTableWidgetItem *item = new QTableWidgetItem(str);
        tableWidget->setItem(row, col, item);
    }
}

FileNameDialog::~FileNameDialog()
{
}

void FileNameDialog::setDir(void)
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                    m_dir,
                                                    QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);
    if( !dir.isNull() )
    {
        m_dir = dir;
        lineEditDir->setText(m_dir);
    }
}
